package com.xof.phonecall.telecom

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.telecom.PhoneAccount
import android.telecom.PhoneAccountHandle
import android.telecom.TelecomManager
import com.xof.phonecall.MyApplication
import com.xof.phonecall.R.string.app_name

object MyPhoneAccountManager {
    var telecomManager: TelecomManager
    var phoneAccountId: String
    var componentName: ComponentName
    var phoneAccountHandle: PhoneAccountHandle
    var phoneAccount: PhoneAccount

    const val EXTRA_CALLER_NAME: String = "EXTRA_CALLER_NAME"
    const val EXTRA_CALL_UUID: String = "EXTRA_CALL_UUID"
    const val EXTRA_CALL_NUMBER: String = "EXTRA_CALL_NUMBER"

    init {
        telecomManager =
            MyApplication.context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        phoneAccountId = MyApplication.context.packageName
        componentName = ComponentName(MyApplication.context, MyConnectionService::class.java)
        phoneAccountHandle = PhoneAccountHandle(componentName, phoneAccountId)
//        phoneAccount = telecomManager.getPhoneAccount(phoneAccountHandle)
//            val uri = Uri.parse("tel:987654321")
        val capabilities = PhoneAccount.CAPABILITY_SELF_MANAGED
        phoneAccount =
            PhoneAccount.builder(phoneAccountHandle, app_name.toString())
                .setCapabilities(capabilities)
                .build()
        telecomManager.registerPhoneAccount(phoneAccount)

//        }
//        if (!phoneAccount.isEnabled) {
//            telecomManager.registerPhoneAccount(phoneAccount)
//        }
    }

    fun makeInit() {

    }

    private fun isPhoneAccountEnable(): Boolean {
        return phoneAccount.isEnabled
    }

    fun displayNewIncomingCall(phoneNumber: String) {
        if (!isPhoneAccountEnable()) {
            return
        }
        //传递一些必要的信息
        val extras = Bundle()
        val uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null)
        extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS, uri)

        //调用addNewIncomingCall添加来电
        telecomManager.addNewIncomingCall(phoneAccountHandle, extras)
    }


    @SuppressLint("MissingPermission")
    fun startCall(uuid: String?, number: String?, callerName: String?) {
//        if (!isPhoneAccountEnable()) {
//            println("isPhoneAccountEnable")
//            return
//        }
        //传递一些必要的信息
        val extras = Bundle()
        val uri = Uri.parse("tel:$number")
        val callExtras = Bundle()
        callExtras.putString(EXTRA_CALLER_NAME, callerName)
        callExtras.putString(EXTRA_CALL_UUID, uuid)
        callExtras.putString(EXTRA_CALL_NUMBER, number)
        extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle)
        extras.putParcelable(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, callExtras)

        //调用addNewIncomingCall添加去电
        telecomManager.placeCall(uri, extras)
    }

}